﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace StoveTop
{
	// Fragment logic for viewing a recipe step
	public class StepFragment : Fragment
	{
		// Constants to keep track of UI elements
		private static string NUMBER = "num";
		private static string INSTRUCTIONS = "step";

		private int number;
		private string instructions;

		private TextView StepNumberView;
		private TextView InstructionView;

		private GestureDetector gestures;

		public event EventHandler PrevEvent;
		public event EventHandler NextEvent;
	
		public static StepFragment CreateStep(int number, string instructions)
		{
			StepFragment fragment = new StepFragment ();
			fragment.Arguments = new Bundle ();
			fragment.Arguments.PutInt (NUMBER, number);
			fragment.Arguments.PutString (INSTRUCTIONS, instructions);
			return fragment;
		}

		public override void OnCreate (Bundle savedInstanceState)
		{
			base.OnCreate (savedInstanceState);

			if (this.Arguments != null)
			{
				this.number = this.Arguments.GetInt (NUMBER);
				this.instructions = this.Arguments.GetString (INSTRUCTIONS);
			}

			this.StepNumberView = new TextView (this.Activity);
			this.InstructionView = new TextView (this.Activity);

			this.StepNumberView.Text = "STEP " + this.number;
			this.StepNumberView.Gravity = GravityFlags.CenterHorizontal;
			this.StepNumberView.SetTextSize (ComplexUnitType.Pt, 18);

			this.InstructionView.Text = this.instructions;
			this.InstructionView.Gravity = GravityFlags.CenterHorizontal;
			this.InstructionView.SetTextSize (ComplexUnitType.Pt, 12);

			this.gestures = new GestureDetector (new GestureListener (this.HandleRight, this.HandleLeft));
		}

		public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
		{
			LinearLayout layout = new LinearLayout (this.Activity);
			layout.Orientation = Orientation.Vertical;

			layout.AddView (this.StepNumberView);
			layout.AddView (this.InstructionView);
			layout.SetOnTouchListener (new TouchListener(this.gestures));

			return layout;
		}

		private void HandleRight()
		{
			if(this.NextEvent != null)
			{
				this.NextEvent (this, new EventArgs());
			}
		}

		private void HandleLeft()
		{
			Console.WriteLine ("LEFT FIRED");
			if(this.PrevEvent != null)
			{
				Console.WriteLine ("PREV HANDLED");
				this.PrevEvent (this, new EventArgs());
			}
		}
	}

	class TouchListener : Java.Lang.Object, View.IOnTouchListener
	{
		private GestureDetector detector;

		public TouchListener(GestureDetector detector)
		{
			this.detector = detector;
		}

		public bool OnTouch (View v, MotionEvent e)
		{
			this.detector.OnTouchEvent (e);
			return true;
		}
	}

	class GestureListener : GestureDetector.SimpleOnGestureListener 
	{
		private Action right;
		private Action left;

		public GestureListener(Action right, Action left)
		{
			this.right = right;
			this.left = left;
		}

		public override bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 
		{
			if (velocityX <= -400)
			{
				this.right ();
				Console.WriteLine ("RIGHT");
				return false;
			}

			if (velocityX >= 400)
			{
				this.left ();
				Console.WriteLine ("LEFT");
				return false;
			}

			return false;
		}
	}
}

